package com.kedsoft.andaily.controller;


import com.kedsoft.andaily.common.MessageResult;
import com.kedsoft.andaily.entity.Category;
import com.kedsoft.andaily.service.CategoryService;
import com.kedsoft.andaily.service.RuleService;
import com.kedsoft.andaily.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-29
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RuleService ruleService;

    @RequestMapping(value = "index")
    public String index() {
        return "category/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Category> getPageList(Pagination<Category> pagination, @RequestParam(required = false) String title){
        Map<String, Object> query = new HashMap<>();
        query.put("title", title);
        categoryService.pageList(pagination,query);
        PageVo<Category> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<CategorySelectVo> listAll() {
        return categoryService.getAll();
    }

    @RequestMapping(value = "form")
    public String form(ModelMap map){
        List<CategorySelectVo> list = categoryService.getAll();
        map.addAttribute("category",list);
        return "category/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(CategoryVo param){
        Category bean = new Category();
        BeanUtils.copyProperties(param,bean);
        if(param.getPid()!=0){
            Category p = categoryService.selectById(param.getPid());
            bean.setPtitle(p.getTitle());
        }else{
            bean.setPtitle("-");
        }
        bean.setCrtime(new Date());
        categoryService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(CategoryVo param){
        Category bean = new Category();
        BeanUtils.copyProperties(param,bean);
        if(param.getPid()!=0){
            Category p = categoryService.selectById(param.getPid());
            bean.setPtitle(p.getTitle());
        }else{
            bean.setPtitle("-");
        }
        bean.setUptime(new Date());
        categoryService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        categoryService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            categoryService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "content")
    public String content(@RequestParam int id,ModelMap map){
        Category bean = categoryService.selectById(id);
        map.put("bean",bean);
        return "category/content";
    }

    @RequestMapping(value = "savetext")
    @ResponseBody
    public MessageResult savetext(@RequestParam int id, @RequestParam String content){
        Category param = new Category();
        param.setId(id);
        param.setContent(content);
        param.setUptime(new Date());
        categoryService.updateById(param);
        return MessageResult.getSuccess("内容添加成功");
    }

}

